from typing import *


class Solution:

    def maxSum(self, nums: List[int], m: int, k: int) -> int:
        d = DefaultDict(int)
        ans = sum = 0
        for i in range(len(nums)):
            d[nums[i]] += 1
            sum += nums[i]
            if i - k + 1 >= 0:
                if len(d) > m:
                    ans = max(ans, sum)
                sum -= nums[i - k + 1]
                d[nums[i - k + 1]] -= 1
                if d[nums[i - k + 1]] == 0:
                    d.pop(nums[i - k + 1])
        return ans